#version 330

in vec2 vUV;
in vec3 vP;
in vec4 vColor;

out vec4 fColor;

uniform sampler2D tex;
uniform sampler2D bumpTex;
uniform float osg_FrameTime;

vec3 L = vec3(0, 0, -1);
void main()
{
	vec2 uv1 = vUV * 1.0 + osg_FrameTime * 0.01;
	vec2 uv2 = vUV * 0.5 + osg_FrameTime * 0.02;
	vec2 uv3 = vUV * 0.25 + osg_FrameTime * 0.01;
	vec2 uv4 = vUV * 0.125 + osg_FrameTime * 0.02;

	vec3 bumpTex1 = texture(bumpTex, uv1).xyz;
	vec3 bumpTex2 = texture(bumpTex, uv2).xyz;
	vec3 bumpTex3 = texture(bumpTex, uv3).xyz;
	vec3 bumpTex4 = texture(bumpTex, uv4).xyz;
	
	vec3 N = normalize(2.0*(bumpTex1 + bumpTex2 + bumpTex3 + bumpTex4) - 4.0);

	float facing = clamp(dot(-vP, N), 0, 1);

	fColor.rgb = mix(vec3(0, 0.1, 0.15), vec3(0.4, 0.6, 0.8), facing);

	fColor.rgb += vec3(1) * max(0, pow(dot(reflect(L,N), -vP), 100));

	fColor.a = 0.5;
}